package com.hxd.otc.module.fiat_deal.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseAdapter;
import com.hxd.otc.module.fiat_deal.bean.BaseFilterBean;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzy on 2018/12/26 0026.
 * description ${筛选过滤Adapter}
 */
public class FilterAdapter extends BaseAdapter<FilterAdapter.FilterHolder, BaseFilterBean> {
    public static final int DEFAULT_POSITION = -1;
    private int mCurPosition = DEFAULT_POSITION;

    public FilterAdapter(Context context) {
        super(context);
    }

    @Override
    protected void onBindHolder(@NonNull final FilterHolder holder, final int position) {
        BaseFilterBean item = getItem(position);
        holder.tvItem.setText(item.getText());
        holder.tvItem.setSelected(mCurPosition != -1 && mCurPosition == position);
        holder.tvItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCurPosition == position) return;
                int prePosition = mCurPosition;
                mCurPosition = position;
                if (prePosition != -1) {
                    notifyItemChanged(prePosition);
                }
                notifyItemChanged(mCurPosition);
            }
        });
    }

    @NonNull
    @Override
    public FilterHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new FilterHolder(mInflater.inflate(R.layout.item_filter, parent, false));
    }

    static class FilterHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_item)
        TextView tvItem;

        FilterHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public BaseFilterBean getCurItem() {
        return getItem(mCurPosition);
    }

    public void setCurPosition(int position) {
        mCurPosition = position;
        notifyDataSetChanged();
    }
}
